﻿#include "EditorDebugAndRunAbilityGroupViewModel.h"

#include "Asset/EditorDefaultPackage.h"
#include "EditorDebugAndRunAbilityGroupModel.h"
#include "Widgets/Window.h"

TSharedObjectPtr<RTexture2D> REditorDebugAndRunAbilityGroupViewModel::GetIcon() const
{
    return Icon;
}

TSharedObjectPtr<RTexture2D> REditorDebugAndRunAbilityGroupViewModel::GetHighlightIcon() const
{
    return HighlightIcon;
}

void REditorDebugAndRunAbilityGroupViewModel::StartDebug()
{
    Model->Launch();
}

void REditorDebugAndRunAbilityGroupViewModel::Compile()
{
    Model->Compile();
}

bool REditorDebugAndRunAbilityGroupViewModel::GetDebuggingBarVisible()
{
    return Model->GetClientState() == EDebugClientState::Connected
        || Model->GetClientState() == EDebugClientState::Paused
    ;
}

float REditorDebugAndRunAbilityGroupViewModel::GetDebugBarInitialX(TSharedObjectPtr<RWindow> InWindow)
{
    return InWindow->GetWindowSize().X-DebugBarWidth-20;
}

float REditorDebugAndRunAbilityGroupViewModel::GetDebugBarInitialY()
{
    return MaxDebugBarY;
}

float REditorDebugAndRunAbilityGroupViewModel::GetDebugBarMaxX(TSharedObjectPtr<RWindow> InWindow)
{
    return InWindow->GetWindowSize().X-DebugBarWidth;
}

TSharedObjectPtr<RTexture2D> REditorDebugAndRunAbilityGroupViewModel::GetContinuePauseIcon() const
{
    return Model->GetClientState() == 
        EDebugClientState::Paused ? ContinueIcon : PauseIcon;
}

CString REditorDebugAndRunAbilityGroupViewModel::GetContinuePauseToolTip() const
{
    return Model->GetClientState() == 
        EDebugClientState::Paused ? U"继续" : U"暂停";
}

void REditorDebugAndRunAbilityGroupViewModel::OnContinuePauseClicked()
{
    if(Model->GetClientState() == EDebugClientState::Paused)
    {
        Model->Continue();
    }
    else
    {
        Model->Pause();
    }
}

void REditorDebugAndRunAbilityGroupViewModel::StopDebug()
{
    Model->Stop();
}



void REditorDebugAndRunAbilityGroupViewModel::BindModel(TSharedObjectPtr<REditorAbilityModel> InModel)
{
    Super::BindModel(InModel);
    Model = InModel;

    Icon = REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugAlt");
    HighlightIcon = REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugAlt_Highlight");

    StartDebugIcon = REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugStart");
    CompileIcon= REditorDefaultPackage::Get()->GetIcon(U"Compile");
    GripIcon = REditorDefaultPackage::Get()->GetIcon(U"CodiconGripper");
    ContinueIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugContinue");
    StopIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugStop");
    StepOverIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugStepOver");
    StepIntoIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugStepInto");
    StepOutIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugStepOut");
    RestartIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugRestart");
    PauseIcon= REditorDefaultPackage::Get()->GetIcon(U"CodiconDebugPause");

}


CString REditorDebugAndRunAbilityGroupViewModel::GetCurrentConfigurationName() const
{
    return ToConfigurationName(Model->DebugConfiguration);
}

void REditorDebugAndRunAbilityGroupViewModel::SetCurrentConfigurationName(CString InName)
{
    if(InName == U"调试项目")
    {
        Model->DebugConfiguration=EDebugConfiguration::Project;
    }
    else if(InName == U"附加到项目")
    {
        Model->DebugConfiguration=EDebugConfiguration::AttachToProject;
    }
    else
    {
        assert(false);
    }
}

TVector<CString> REditorDebugAndRunAbilityGroupViewModel::GetConfigurationNames() const
{
    return {U"调试项目", U"附加到项目"};
}

uint16_t REditorDebugAndRunAbilityGroupViewModel::GetPort() const
{
    return Model->DefaultPort;
}

void REditorDebugAndRunAbilityGroupViewModel::SetPort(uint16_t InPort)
{
    Model->DefaultPort = InPort;
}

CString REditorDebugAndRunAbilityGroupViewModel::ToConfigurationName(EDebugConfiguration InConfiguration) const
{
    switch (InConfiguration)
    {
        case EDebugConfiguration::Project:
            return U"调试项目";
        case EDebugConfiguration::AttachToProject:
            return U"附加到项目";
        default:
            assert(false);
            return U"";
    }
}
